package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lx
 * @Time: 11:27
 * @Description:
 * 后台管理
 */
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin(origins = {"http://api.gmall.com","http://api.taobao.com"})
//@CrossOrigin
public class ManagerController {

    @Autowired
    private ManagerService managerService;
    //查询一级分类
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        //查询所有一级分类集合
        List<BaseCategory1> list = managerService.getCategory1();
        return Result.ok(list);
    }
    //查询二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> list = managerService.getCategory2(category1Id);
        return Result.ok(list);
    }
    //查询三级分类
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> list = managerService.getCategory3(category2Id);
        return Result.ok(list);
    }
    //根据一二三级分类ID 查询平台属性及属性值集合
    //返回值 Result  data  = List<BaseAttrInfo> 平台属性集合
    // BaseAttrInfo 平台属性对象 里面有平台属性值集合
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(category1Id,
                category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }
    //保存平台属性及属性值
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
    //查询品牌分页列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit
                                ){
        //分页对象
        IPage<BaseTrademark> baseTrademarkIPage = managerService.baseTrademark(page,limit);
        return Result.ok(baseTrademarkIPage);
    }
    //去修改品牌
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademark(@PathVariable Long id){
        BaseTrademark baseTrademark = managerService.getBaseTrademark(id);
        return Result.ok(baseTrademark);
    }
    //提交修改品牌
    // restful 风格开发原则  ：
    // 添加  post
    // 查询 get
    // 修改 put
    // 删除  delete
    @PutMapping("/baseTrademark/update")
    public Result baseTrademarkUpdate(@RequestBody BaseTrademark trademark){
        managerService.baseTrademarkUpdate(trademark);
        return Result.ok();
    }

    //商品分页列表
    @GetMapping("/{page}/{limit}")
    public Result spuList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id
    ){
        IPage<SpuInfo> spuInfoIPage = managerService.spuList(page,limit,category3Id);
        return Result.ok(spuInfoIPage);
    }
    //查询所有品牌不分页 列表
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //查询销售属性字典表
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    //添加SPU
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //根据商品ID查询所有图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList =  managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }
    //根据商品ID查询 销售属性及属性值集合
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuImageList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuImageList);
    }
    //保存SKU
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    //查询SKU分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result list(
            @PathVariable Integer page,
            @PathVariable Integer limit
    ){
        IPage<SkuInfo> skuInfoIPage = managerService.list(page,limit);
        return Result.ok(skuInfoIPage);
    }
    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        managerService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
